//
//  FJTextInputAccessoryView.m
//  OnionHome
//
//  Created by fengjie on 2019/10/15.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "FJTextInputAccessoryView.h"
#import "RZRichTextInputAVCell.h"
#import "RZRichTextConfigureManager.h"
#import "RZRichTextInputFontColorCell.h"
#import "RZRichTextInputFontBgColorCell.h"

#import "FJEditorInputView.h"

@interface FJTextInputAccessoryView ()

@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, assign) CGFloat keyBoardHeight;

/**<#属性#>*/
@property (nonatomic,strong)FJEditorInputView*inputView;
/**<#属性#>*/
@property (nonatomic,strong)UIButton*oneBtn;
/**<#属性#>*/
@property (nonatomic,strong)UIButton*twoBtn;
/**<#属性#>*/
@property (nonatomic,strong)UIButton*thereBtn;


@end


@implementation FJTextInputAccessoryView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(0xF5F7F5);
        
        [self oneBtn];
        [self twoBtn];
        [self thereBtn];

        [self.oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.width.height.equalTo(@44);
        }];
        [self.twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.top.equalTo(self);
            make.width.height.equalTo(@44);
        }];
        [self.thereBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.equalTo(self);
            make.width.height.equalTo(@44);
        }];

        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeItemKeyboard) name:UIDeviceOrientationDidChangeNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardInfo:) name:UIKeyboardDidChangeFrameNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_closeBtn];
        [_closeBtn setImage:k_rz_richImage(@"rz_jp_c") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
// 关闭键盘
- (void)closeKeyboard {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)closeItemKeyboard {
    
}
- (void)keyboardInfo:(NSNotification *)notification {
    NSValue *value = notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"];
    CGRect frame = value.CGRectValue;
    self.keyBoardHeight = frame.size.height - 44; // 工具条还有44的高度需要减去
}

- (UIButton *)oneBtn {
    if (!_oneBtn) {
        _oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_oneBtn];
        [_oneBtn setImage:IMAGE_NAMED(@"rz_image") forState:UIControlStateNormal];
        kWeakSelf(weakSelf);
        [_oneBtn addActionHandler:^{
            if (weakSelf.didClcikedAttrItemIndex) {
                weakSelf.didClcikedAttrItemIndex(0);
            }
        }];
    }
    return _oneBtn;
}
- (UIButton *)twoBtn {
    if (!_twoBtn) {
        _twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_twoBtn];
        [_twoBtn setImage:IMAGE_NAMED(@"rz_markup_d") forState:UIControlStateNormal];
        kWeakSelf(weakSelf);
        [_twoBtn addActionHandler:^{
            [weakSelf twoBttonAction];
        }];

    }
    return _twoBtn;
}
- (UIButton *)thereBtn {
    if (!_thereBtn) {
        _thereBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_thereBtn];
        [_thereBtn setImage:IMAGE_NAMED(@"zhuanfa_1") forState:UIControlStateNormal];
        kWeakSelf(weakSelf);
        [_thereBtn addActionHandler:^{
            [weakSelf thereButtonAction];
        }];
    }
    return _thereBtn;
}
-(FJEditorInputView*)inputView{
    if(!_inputView){
        CGSize size = [UIScreen mainScreen].bounds.size;
        _inputView=[[FJEditorInputView alloc] initWithFrame:CGRectMake(0, size.height, size.width, 0)];
        kWeakSelf(weakSelf);
        _inputView.completeBlock = ^{
            [weakSelf twoBttonAction];
        };
        
        _inputView.selectCellBlock = ^(FJEditorInputStyleModel * _Nonnull model) {
            BlockSafeRun(weakSelf.didFontStyleAttrItemIndex,model.fontValue);
            [weakSelf twoBttonAction];
        };
    }
    return _inputView;
}
-(void)twoBttonAction{
    
    if (self.thereBtn.selected) {
        CGSize size = [UIScreen mainScreen].bounds.size;
        UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
        CGRect rect = [self convertRect:self.frame toView:window];
        CGFloat height = size.height - CGRectGetMaxY(rect);
        self.thereBtn.selected = !self.thereBtn.selected;
        self.inputView.frame = CGRectMake(0, size.height, size.width, height);
        [self.inputView removeFromSuperview];
    }
    
    self.inputView.type = FJEditorInputViewStyle;
    self.twoBtn.selected = !self.twoBtn.selected;
    [self showCustomKeyboard:self.twoBtn.selected];
}
-(void)thereButtonAction{
    self.inputView.type = FJEditorInputViewLine;
    self.thereBtn.selected = !self.thereBtn.selected;
    [[UIApplication sharedApplication].keyWindow endEditing:YES];

//    [self showCustomKeyboard:self.thereBtn.selected];
}
-(void)showCustomKeyboard:(BOOL)value{
    
    if (value) {
        if (self.inputView.superview) {
            return;
        }
        [self setEditorInputViewFrameWithShowOrHide:NO];
        [[UIApplication sharedApplication].windows.lastObject addSubview:self.inputView];
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [self setEditorInputViewFrameWithShowOrHide:YES];
        } completion:nil];
        
    }else{
        if (!self.inputView.superview) {
            return;
        }
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            [self setEditorInputViewFrameWithShowOrHide:YES];
        } completion:^(BOOL finished) {
            [self.inputView removeFromSuperview];
        }];

    }
}

- (void)setEditorInputViewFrameWithShowOrHide:(BOOL)isShow {
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
    CGRect rect = [self convertRect:self.frame toView:window];
    
    CGFloat height = size.height - CGRectGetMaxY(rect);
    
    if (isShow) {
        if (self.inputView.type == FJEditorInputViewStyle) {
            self.inputView.frame = CGRectMake(0, CGRectGetMaxY(rect)-44, size.width, height+44);
        }else{
            self.inputView.frame = CGRectMake(0, CGRectGetMaxY(rect), size.width, height);
        }
    } else {
        self.inputView.frame = CGRectMake(0, size.height, size.width, height);
    }
}

@end
